<template>
  <div class="login">
    <div class="loginBox">
      <div class="img"><img src="../assets/images/4.jpg" alt="" /></div>

      <!-- elementUI-表单域 -->
      <el-form
        ref="loginForm"
        :rules="rules"
        :model="loginForm"
        label-width="70px"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名:" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码:" prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-button type="primary" class="loginBtn" round @click="login"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from "@/apis/user.js";

export default {
  // 数据：
  data() {
    return {
      //  表单对象：
      loginForm: {
        username: "",
        password: "",
      },
      // 正则：
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "用户名需在3-12之间", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  //   方法：
  methods: {
    login() {
      //  validate是验证正则的方法：
      this.$refs.loginForm.validate(async (valid) => {
        //  如果验证成功，才发送登录请求
        if (valid) {
          //   如果登录正常，则正常运行
          try {
            console.log(this.loginForm);
            let res = await userLogin(this.loginForm);
            console.log(res);
            // 登录成功后,将用户token存入本地
            localStorage.setItem("admin_tokon", res.data.data.token);
            // 恭喜用户
            this.$message({
              message: "登录成功",
              type: "success",
              duration: 1000,
              center: true,
            });
            // 跳转到首页
            setTimeout(() => {
              this.$router.push({ path: "/index" });
            }, 600);
            //   如果登录失败, 抛出错误 提示给用户看
          } catch (e) {
            this.$message({
              message: e.response.data.message,
              type: "error",
              duration: 2000,
              center: true,
            });
          }
        } else {
          this.$message({
            message: "没有输入用户名或密码",
            type: "warning",
            duration: 2000,
            center: true,
          });
        }
      });
    },
  },
};
</script>

// 样式：
<style lang="less" scoped>
* {
  box-sizing: border-box;
}
.login {
  display: flex;
  justify-content: center;
  padding-top: 160px;
}
.loginBox {
  position: relative;
  display: block;
  width: 500px;
  height: 340px;
  background: #efefef;
  padding: 0 30px 0 15px;
  padding-top: 80px;
}
.loginBtn {
  margin-top: 20px;
  width: 100%;
  font-size: 20px;
}
.img {
  position: absolute;
  top: -50px;
  left: 200px;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
  }
}
</style>